﻿using System;
using System.Linq;
using System.Text;
using Irony.Ast;
using Irony.Parsing;
using LibraryGuard.Interfaces;

namespace LibraryGuard.Internals.AST
{
	/// <summary>
	/// Represents code.
	/// </summary>
	internal class Code
		: IValue
	{
		#region Fields
		private readonly IValue[] Values = null;
		#endregion

		#region Constructors
		public Code(IValue[] values)
		{
			this.Values = values;
		}
		#endregion

		#region IValue Members
		/// <inheritdoc />
		public TypeCode GetBaseType(IDataProvider data)
		{
			return TypeCode.String;
		}

		/// <inheritdoc />
		public bool CanBeConvertedTo(TypeCode type, IDataProvider data)
		{
			return type == TypeCode.String;
		}

		/// <inheritdoc />
		public object GetValue(TypeCode type, IDataProvider data)
		{
			StringBuilder builder = new StringBuilder();
			foreach (var val in this.Values)
				builder.Append(val.GetValue(TypeCode.String, data));
			return builder.ToString();
		}
		#endregion

		#region Statics
		public static void Create(AstContext context, ParseTreeNode parseNode)
		{
			parseNode.AstNode = new Code(parseNode.ChildNodes.Select(n => n.AstNode).Cast<IValue>().ToArray());
		}
		#endregion
	}
}
